﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.Web.UI.HtmlControls;

namespace OnlineCellphoneStore
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["ClientID"] == null)
                {
                    AnonymousContext();
                }
                else
                {
                    UserContext();
                }
                FillTree();
            }

        }
        private void FillTree()
        {
            DAL.DBAdapter.TreeViewCellphonesDataTable dt = SQLAdapter.GetTreeData();

            var qry = from c in dt.AsEnumerable()
                      orderby c.Field<string>("OSCategoryName")
                      select c;

            DataTable TempTable = qry.AsDataView().ToTable();
            DataTable Dv = RemoveDuplicatesRecords(TempTable);
            string Os = Dv.Rows[0][0].ToString();
            string Manufacturers = Dv.Rows[0][1].ToString();
            TreeNode OsTreeNode = new TreeNode(Os);
            TreeNode ManufacturersTreeNode = new TreeNode(Dv.Rows[0][1].ToString());
            for (int i = 0; i < Dv.Rows.Count; i++)
            {
                // New os node
                if (Dv.Rows[i][0].ToString() != Os)
                {
                    // Add the current os finished node to tree nodes
                    OsTreeNode.ChildNodes.Add(ManufacturersTreeNode);
                    MainTreeView.Nodes.Add(OsTreeNode);
                    Os = Dv.Rows[i][0].ToString();
                    Manufacturers = Dv.Rows[i][1].ToString();
                    OsTreeNode = new TreeNode(Os);
                    ManufacturersTreeNode = new TreeNode(Manufacturers);
                    ManufacturersTreeNode.ChildNodes.Add(new TreeNode(Dv.Rows[i][2].ToString()));
                }
                else
                {
                    // Check if manu changed
                    if (Dv.Rows[i][1].ToString() != Manufacturers)
                    {
                        OsTreeNode.ChildNodes.Add(ManufacturersTreeNode);
                        Manufacturers = Dv.Rows[i][1].ToString();
                        ManufacturersTreeNode = new TreeNode(Manufacturers);
                        ManufacturersTreeNode.ChildNodes.Add(new TreeNode(Dv.Rows[i][2].ToString()));
                    }
                    // Else its a regular row just need to add the device to manu
                    else
                    {
                        ManufacturersTreeNode.ChildNodes.Add(new TreeNode(Dv.Rows[i][2].ToString()));
                    }
                }
            }
            OsTreeNode.ChildNodes.Add(ManufacturersTreeNode);
            MainTreeView.Nodes.Add(OsTreeNode);
            MainTreeView.CollapseAll();
        }
        private DataTable RemoveDuplicatesRecords(DataTable dt)
        {
            // Returns just 5 unique rows
            var UniqueRows = dt.AsEnumerable().Distinct(DataRowComparer.Default);
            DataTable dt2 = UniqueRows.CopyToDataTable();
            return dt2;
        }
        protected void LogOut_Click(object sender, EventArgs e)
        {
            string Url = "Main.aspx?LogOut=true";
            Response.Redirect(Url, false);
        }
        private void AnonymousContext()
        {
            HandleAnonymousLables();
        }
        private void HandleAnonymousLables()
        {
            ShopingCartLink.Attributes.Add("href", "Account/Login.aspx");
        }
        private void UserContext()
        {
            HandleUserLables();
            AddPurchaseNavigator();
        }
        private void AddPurchaseNavigator()
        {
            HtmlGenericControl li = new HtmlGenericControl("li");
            HtmlGenericControl anchor = new HtmlGenericControl("a");
            HtmlGenericControl span = new HtmlGenericControl("span");
            HtmlGenericControl text = new HtmlGenericControl("span");

            string Url = "UserPages/PreviousPurchase.aspx";
            anchor.ID = "RecentPurchasesLink";
            anchor.Attributes.Add("href", Url);
            text.InnerText = " My Previous Purchase";
            span.Attributes.Add("class", "glyphicon glyphicon-th-large");

            anchor.Controls.Add(span);
            anchor.Controls.Add(text);
            li.Controls.Add(anchor);
            NavBar.Controls.Add(li);
        }
        private void HandleUserLables()
        {
            ShopingCartLink.Attributes.Add("href", "UserPages/ShopingCart.aspx");
            anonymousUserOptions.Attributes.CssStyle.Value = "display: none;";
            registerdUserOptions.Attributes.CssStyle.Value = "display: inline;";
            aHelloUser.InnerHtml += Session["UserName"].ToString();
            aLastEntered.InnerHtml += Session["LastVisit"].ToString();
        }
        protected void BtnBackToMain_Click(object sender, EventArgs e)
        {
            string Url = "Main.aspx";
            Response.Redirect(Url, false);
        }

    }
}